In this tutorial of C++, we will discuss C++ constants or literals. Here we have provided a brief definition of literals with appropriate examples.
C++ Constants/Literals
In C++, the term constants and literals are referred to the fixed values, which may not be changed throughout the program. We interchangeably use the term Constants and Literals in C++. On the basis of Data types, we have divided constants into 5 major types, namely Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values. Constants or literals are like normal variables but their values cannot be changed throughout the program and to represent a constant lateral we use the const keyword. Example:
#include <iostream> using namespace std; int main() { const int x = 100; //constant literal x with literal value 5 }
Behind the code: In this program, x is the literal variable and 100 is the literal value.
Types of Literals in C++
- Integer Literals
- Float Literals
- Character Literals
- String Literals
- Boolean Literals
1. Integer Literals
These are used to represent and store the integer values, which can be a decimal, octal or hexadecimal constant. Example:
#include <iostream> using namespace std; int main() { const int val = 10; // constant integer literal cout << "Integer Literal: "<< val; return 0; }
2. Floating Points Literals:
With Floating point Laterals, we can represent and store real numbers. The real number includes integer, real, fractional and even exponential. With floating points literal we can deal with decimal numbers, which means we can represent a floating-point literal either with a decimal number or exponent. Example:
#include <iostream> using namespace std; int main() { const float val = 7.14; // Floating-point literal cout << "Floating-point literal: " << val ; return 0; }
3. Character Literal
A character literal is used to represent and store a single character using a single quote. It could be anything except the escape sequence. A character could be a letter, digit, or any special symbol. Example:
#include <iostream> using namespace std; int main() { const char ch = 'A'; // constant char literal cout << "Character Literal: "<< ch; return 0; }
Boolean Literals:
Boolean literals are used to represent and store two values that are true and false. They can store boolean data type values. Example:
#include <iostream> using namespace std; int main() { const bool a = true; const bool b= false; cout << "a is: "<< a<<"\n"; cout << "b is: "<< b; return 0; }
Conclusion
Constants and literals in C++ are values of a variable that we cannot change throughout the program. We can define a constant or a literal using the const keyword. In this article, we have discussed all the five types of constants and literals in C++ with appropriate examples to help you develop a better understanding of them. Happy Learning! People are also reading:
- WAP in C++ & Python for the Union of Two Arrays
- C++ Basic Input/Output
- WAP in C++ & Python & sort an Array Using Bubble Sort
- Files and Streams in C++
- WAP to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2
- C++ Loop Types
- WAP in C++ and Python to find the sum of the series x+ x2/2 +x3/3 + ……. +x
- Data Structures in C++
- WAP to find the divisors of a Positive Integer
- Functions in C++